package com.sgb.kjwl.view.ui.activity.transport.driver.trans;

import android.content.Intent;
import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.CarInfo;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.self.CarriageSelectEntity;
import com.swgk.core.view.entity.DriverAuthDetailEntity;
import com.sgb.kjwl.model.entity.transport.CarrierPushCarrierEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.UserAddressEntity;
import com.sgb.kjwl.repository.TransportRepertory;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by harisucici on 2020/4/7
 * Class description:
 */
public class TransPushModel extends BaseViewModel {
    public ObservableField<UserAddressEntity> userAddressEntity = new ObservableField<>(new UserAddressEntity());
    public ObservableField<CarrierPushCarrierEntity> carrierPushCarrierEntity = new ObservableField<>(new CarrierPushCarrierEntity());

    public ObservableField<Long> carTypes = new ObservableField<>(0L); // 自卸
    private TransPushActivity activity;
    private TransportRepertory repertory;
    public List<GoodsInfoTypeEntity3> payType;

    public TransPushModel(TransPushActivity activity, TransportRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

   /* public void getGoodsTypeData() {

        String carsTypeString = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.CARS_INFO_TYPE, "");
        String carsLengthString = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.CARS_INFO_LENGTH, "");
        List<GoodsInfoTypeEntity3> carsType = new Gson().fromJson(carsTypeString, new TypeToken<List<GoodsInfoTypeEntity3>>() {
        }.getType());
        List<GoodsInfoTypeEntity3> carsLengthType = new Gson().fromJson(carsLengthString, new TypeToken<List<GoodsInfoTypeEntity3>>() {
        }.getType());
        activity.getCarsTypeData(carsType);
        activity.getCarsLengthData(carsLengthType);

    }

    *//* 展示司机 数据*//*
    public void driverAuthDetail() {
        DialogHelper.showProgressDialog(activity, null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().driverAuthDetail().enqueue(new Callback<BaseEntity<DriverAuthDetailEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<DriverAuthDetailEntity>> call, Response<BaseEntity<DriverAuthDetailEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null)
                    return;
                MLog.v("展示司机数据 ", response.message());
                activity.getDriverInfo(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<DriverAuthDetailEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }*/
   public void getUserInfo() {
       DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
       APIManager.getInstance().getUserAPI().getUserInfo().enqueue(new Callback<BaseEntity<UserMsgEntity>>() {
           @Override
           public void onResponse(Call<BaseEntity<UserMsgEntity>> call, Response<BaseEntity<UserMsgEntity>> response) {
               DialogHelper.dismissProgressDialog();
               if (response.body() == null) {
                   return;
               }
               if (response.body().getData() == null) return;
               if (response.body().getState().equals("ok")) {
                   activity.displayRole(response.body().getData());
               }
           }

           @Override
           public void onFailure(Call<BaseEntity<UserMsgEntity>> call, Throwable t) {
               DialogHelper.dismissProgressDialog();
           }
       });
   }

    /**
     * 所有地址选项
     */
    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                activity.showAddressPop(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

/*    *//**
     * 获取当前id的地址信息
     *//*
    public void getIdAddress(String id) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getUserAPI().searchAddress(id).enqueue(new Callback<BaseEntity<UserAddressEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<UserAddressEntity>> call, Response<BaseEntity<UserAddressEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null && response.body().getState().equals("ok")) {
                    activity.showAddress(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<UserAddressEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }*/

    //付款方式
    public void getPayType() {
        APIManager.getInstance().getDriverAPI().getPayType().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    payType = response.body().getData();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }

    /**
     * 提交推送承运信息
     */
    public void getSaveCarrierPushCarrierData() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getDriverAPI().saveCarrierPushCarrierData(carrierPushCarrierEntity.get()).enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                DialogHelper.dismissProgressDialog();
                String msg = response.body().getMsg();
                if (response.body().getState().equals("ok")) {
                    MToast.showToast(activity, msg);
                    Intent intent = new Intent();
                    intent.putExtra("state", 1);
                    activity.setResult(1, intent);
                    activity.finish();

                } else {
                    MToast.showToast(activity, msg);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    /**
     * 承运信息详情
     */

  /*  public void showCarrierDetail() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getDriverAPI().getCarrierDetail(repertory.getNo(), repertory.getAreaCode()).enqueue(new Callback<BaseEntity<CarrierDetailsEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<CarrierDetailsEntity>> call, Response<BaseEntity<CarrierDetailsEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null)
                    return;
                activity.showDetails(response.body().getData());

            }

            @Override
            public void onFailure(Call<BaseEntity<CarrierDetailsEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }*/
    //获取经纬度信息
    public void getLocationLL(String provinceNo,String cityNo,String districtNo,int type) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().getLocationLL(repertory.getAreaCode(), provinceNo, cityNo, districtNo).enqueue(new Callback<BaseEntity<MapLongAndLatEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<MapLongAndLatEntity>> call, Response<BaseEntity<MapLongAndLatEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.getLocationLL(response.body().getData(),type);
                } else {
                    MToast.showToast(activity.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<MapLongAndLatEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity.getActivity(), t.getMessage());
            }
        });

    }
    //我的车型
    public void carType() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().carType().enqueue(new Callback<BaseEntity<List<CarriageSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CarriageSelectEntity>>> call, Response<BaseEntity<List<CarriageSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showCarModelPop(response.body().getData());
                } else {
                    MToast.showToast(activity.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CarriageSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //我的车长
    public void carLenType() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().carLenType().enqueue(new Callback<BaseEntity<List<CarriageSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CarriageSelectEntity>>> call, Response<BaseEntity<List<CarriageSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showCarLengthPop(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CarriageSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //发票要求
    public void invoiceType() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().invoiceType().enqueue(new Callback<BaseEntity<List<CarriageSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CarriageSelectEntity>>> call, Response<BaseEntity<List<CarriageSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showInvoicePop(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CarriageSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    public void getCarInfo(){
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().getCarInfo().enqueue(new Callback<BaseEntity<List<CarInfo>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CarInfo>>> call, Response<BaseEntity<List<CarInfo>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showCarInfo(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CarInfo>>> call, Throwable t) {

            }
        });
    }

    public void getCarInfo2(){
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().getCarInfo().enqueue(new Callback<BaseEntity<List<CarInfo>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CarInfo>>> call, Response<BaseEntity<List<CarInfo>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showCarInfo2(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CarInfo>>> call, Throwable t) {

            }
        });
    }
    //司机详情
    public void driverAuthDetail() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().driverAuthDetail().enqueue(new Callback<BaseEntity<DriverAuthDetailEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<DriverAuthDetailEntity>> call, Response<BaseEntity<DriverAuthDetailEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.getDriverInfo(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<DriverAuthDetailEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}